﻿using System;
using System.Threading;
using CatClient.Message;
using CatClient.Util;
using CatClient;
using PostSharp.Aspects;
using PostSharp.Aspects.Internals;
using PostSharp.Extensibility;

namespace PsCat.Aspet
{
	// Token: 0x02000007 RID: 7
	[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
	[HasInheritedAttribute(new long[]
	{

	})]
	[Serializable]
	public class CoreCatAttribute : CatBaseAttribute
	{
		// Token: 0x17000004 RID: 4
		// (get) Token: 0x06000014 RID: 20 RVA: 0x000023F3 File Offset: 0x000005F3
		// (set) Token: 0x06000015 RID: 21 RVA: 0x000023FB File Offset: 0x000005FB
		public bool FindMainContext { get; set; }

		// Token: 0x06000016 RID: 22 RVA: 0x00002404 File Offset: 0x00000604
		public CoreCatAttribute(string name = "")
		{
            this._type = "Core";
            this._name = name;
            //base.ApplyToStateMachine = true;
        }

		// Token: 0x06000017 RID: 23 RVA: 0x00002428 File Offset: 0x00000628
		[HasInheritedAttribute(new long[]
		{
			-966559710620335241L
		})]
		[MethodExecutionAdviceOptimization(MethodExecutionAdviceOptimizations.IgnoreSetFlowBehavior | MethodExecutionAdviceOptimizations.IgnoreSetArguments | MethodExecutionAdviceOptimizations.IgnoreGetInstance | MethodExecutionAdviceOptimizations.IgnoreSetInstance | MethodExecutionAdviceOptimizations.IgnoreGetException | MethodExecutionAdviceOptimizations.IgnoreGetReturnValue | MethodExecutionAdviceOptimizations.IgnoreSetReturnValue | MethodExecutionAdviceOptimizations.IgnoreGetMethodExecutionTag | MethodExecutionAdviceOptimizations.IgnoreGetYieldValue | MethodExecutionAdviceOptimizations.IgnoreSetYieldValue | MethodExecutionAdviceOptimizations.IgnoreGetDeclarationIdentifier)]
		public override void OnEntry(MethodExecutionArgs args)
		{
			try
			{
				if (base.AsyncMethod && this.FindMainContext)
				{
					if (!Cat.IsInitializedWithoutSetup())
					{
						return;
					}
					Cat.SetMainContextToCurrent(false);
				}
				else if (!Cat.IsInitialized())
				{
					return;
				}
				if (string.IsNullOrWhiteSpace(this._name))
				{
					this._name = this.NameFormClassMethod(args);
				}
				string nameValuePairs = this.MethodParams(args);
				object methodExecutionTag;
				if (!base.AsyncMethod)
				{
					methodExecutionTag = Cat.NewTransaction(this._type, this._name);
				}
				else
				{
					ITransaction transaction = Cat.NewAsyncTransaction(this._type, this._name);
					methodExecutionTag = transaction;
				}
				args.MethodExecutionTag = methodExecutionTag;
				Cat.LogEvent(this._type, this._name, "0", nameValuePairs);
			}
			catch (Exception ex)
			{
				LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Error(ex.Message, ex);
			}
		}

		// Token: 0x06000018 RID: 24 RVA: 0x00002500 File Offset: 0x00000700
		[HasInheritedAttribute(new long[]
		{
			-966559711310300280L
		})]
		[MethodExecutionAdviceOptimization(MethodExecutionAdviceOptimizations.IgnoreGetMethod | MethodExecutionAdviceOptimizations.IgnoreSetFlowBehavior | MethodExecutionAdviceOptimizations.IgnoreGetArguments | MethodExecutionAdviceOptimizations.IgnoreSetArguments | MethodExecutionAdviceOptimizations.IgnoreGetInstance | MethodExecutionAdviceOptimizations.IgnoreSetInstance | MethodExecutionAdviceOptimizations.IgnoreGetException | MethodExecutionAdviceOptimizations.IgnoreGetReturnValue | MethodExecutionAdviceOptimizations.IgnoreSetReturnValue | MethodExecutionAdviceOptimizations.IgnoreSetMethodExecutionTag | MethodExecutionAdviceOptimizations.IgnoreGetYieldValue | MethodExecutionAdviceOptimizations.IgnoreSetYieldValue | MethodExecutionAdviceOptimizations.IgnoreGetDeclarationIdentifier)]
		public override void OnExit(MethodExecutionArgs args)
		{
			ITransaction transaction = args.MethodExecutionTag as ITransaction;
			if (base.AsyncMethod && !this._yield)
			{
				IAsyncForkedTransaction asyncForkedTransaction = args.MethodExecutionTag as IAsyncForkedTransaction;
				if (asyncForkedTransaction != null)
				{
					asyncForkedTransaction.FreeSecondKey();
					this._yield = true;
				}
			}
			if (transaction == null)
			{
				return;
			}
			transaction.Complete();
		}

		// Token: 0x06000019 RID: 25 RVA: 0x00002550 File Offset: 0x00000750
		[HasInheritedAttribute(new long[]
		{
			-966559710091859626L
		})]
		[MethodExecutionAdviceOptimization(MethodExecutionAdviceOptimizations.IgnoreGetMethod | MethodExecutionAdviceOptimizations.IgnoreSetFlowBehavior | MethodExecutionAdviceOptimizations.IgnoreGetArguments | MethodExecutionAdviceOptimizations.IgnoreSetArguments | MethodExecutionAdviceOptimizations.IgnoreGetInstance | MethodExecutionAdviceOptimizations.IgnoreSetInstance | MethodExecutionAdviceOptimizations.IgnoreGetException | MethodExecutionAdviceOptimizations.IgnoreGetReturnValue | MethodExecutionAdviceOptimizations.IgnoreSetReturnValue | MethodExecutionAdviceOptimizations.IgnoreSetMethodExecutionTag | MethodExecutionAdviceOptimizations.IgnoreGetYieldValue | MethodExecutionAdviceOptimizations.IgnoreSetYieldValue | MethodExecutionAdviceOptimizations.IgnoreGetDeclarationIdentifier)]
		public override void OnSuccess(MethodExecutionArgs args)
		{
			ITransaction transaction = args.MethodExecutionTag as ITransaction;
			if (transaction != null)
			{
				transaction.Status = "0";
			}
		}

		// Token: 0x0600001A RID: 26 RVA: 0x00002578 File Offset: 0x00000778
		[HasInheritedAttribute(new long[]
		{
			-966559711086782217L
		})]
		[MethodExecutionAdviceOptimization(MethodExecutionAdviceOptimizations.IgnoreGetMethod | MethodExecutionAdviceOptimizations.IgnoreSetFlowBehavior | MethodExecutionAdviceOptimizations.IgnoreGetArguments | MethodExecutionAdviceOptimizations.IgnoreSetArguments | MethodExecutionAdviceOptimizations.IgnoreGetInstance | MethodExecutionAdviceOptimizations.IgnoreSetInstance | MethodExecutionAdviceOptimizations.IgnoreGetReturnValue | MethodExecutionAdviceOptimizations.IgnoreSetReturnValue | MethodExecutionAdviceOptimizations.IgnoreSetMethodExecutionTag | MethodExecutionAdviceOptimizations.IgnoreGetYieldValue | MethodExecutionAdviceOptimizations.IgnoreSetYieldValue | MethodExecutionAdviceOptimizations.IgnoreGetDeclarationIdentifier)]
		public override void OnException(MethodExecutionArgs args)
		{
			ITransaction transaction = args.MethodExecutionTag as ITransaction;
			if (transaction == null)
			{
				return;
			}
			if (args.Exception is ThreadAbortException)
			{
				transaction.Status = "0";
				return;
			}
			transaction.SetStatus(args.Exception);
		}

		// Token: 0x0600001B RID: 27 RVA: 0x000025BC File Offset: 0x000007BC
		[HasInheritedAttribute(new long[]
		{
			-966559711827948736L
		})]
		[MethodExecutionAdviceOptimization(MethodExecutionAdviceOptimizations.IgnoreGetMethod | MethodExecutionAdviceOptimizations.IgnoreSetFlowBehavior | MethodExecutionAdviceOptimizations.IgnoreGetArguments | MethodExecutionAdviceOptimizations.IgnoreSetArguments | MethodExecutionAdviceOptimizations.IgnoreGetInstance | MethodExecutionAdviceOptimizations.IgnoreSetInstance | MethodExecutionAdviceOptimizations.IgnoreGetException | MethodExecutionAdviceOptimizations.IgnoreGetReturnValue | MethodExecutionAdviceOptimizations.IgnoreSetReturnValue | MethodExecutionAdviceOptimizations.IgnoreSetMethodExecutionTag | MethodExecutionAdviceOptimizations.IgnoreGetYieldValue | MethodExecutionAdviceOptimizations.IgnoreSetYieldValue | MethodExecutionAdviceOptimizations.IgnoreGetDeclarationIdentifier)]
		public override void OnYield(MethodExecutionArgs args)
		{
			if (base.AsyncMethod && !this._yield)
			{
				IAsyncForkedTransaction asyncForkedTransaction = args.MethodExecutionTag as IAsyncForkedTransaction;
				if (asyncForkedTransaction == null)
				{
					return;
				}
				asyncForkedTransaction.FreeSecondKey();
				this._yield = true;
			}
		}

		// Token: 0x0600001C RID: 28 RVA: 0x000025F8 File Offset: 0x000007F8
		[HasInheritedAttribute(new long[]
		{
			-966559708541291272L
		})]
		[MethodExecutionAdviceOptimization(MethodExecutionAdviceOptimizations.IgnoreGetMethod | MethodExecutionAdviceOptimizations.IgnoreSetFlowBehavior | MethodExecutionAdviceOptimizations.IgnoreGetArguments | MethodExecutionAdviceOptimizations.IgnoreSetArguments | MethodExecutionAdviceOptimizations.IgnoreGetInstance | MethodExecutionAdviceOptimizations.IgnoreSetInstance | MethodExecutionAdviceOptimizations.IgnoreGetException | MethodExecutionAdviceOptimizations.IgnoreGetReturnValue | MethodExecutionAdviceOptimizations.IgnoreSetReturnValue | MethodExecutionAdviceOptimizations.IgnoreSetMethodExecutionTag | MethodExecutionAdviceOptimizations.IgnoreGetYieldValue | MethodExecutionAdviceOptimizations.IgnoreSetYieldValue | MethodExecutionAdviceOptimizations.IgnoreGetDeclarationIdentifier)]
		public override void OnResume(MethodExecutionArgs args)
		{
			if (base.AsyncMethod)
			{
				IAsyncForkedTransaction asyncForkedTransaction = args.MethodExecutionTag as IAsyncForkedTransaction;
				if (asyncForkedTransaction == null)
				{
					return;
				}
				asyncForkedTransaction.Fork();
			}
		}

		// Token: 0x04000006 RID: 6
		private readonly string _type;

		// Token: 0x04000007 RID: 7
		private string _name;

		// Token: 0x04000008 RID: 8
		private bool _yield;
	}
}
